package gentle.base;
import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

/**
 * JSON的驼峰和下划线互转帮助类
 *
 * @author yangzhilong
 *
 */
public class JsonUtils {

	/**
	 * 将对象的大写转换为下划线加小写，例如：userName-->user_name
	 *
	 * @param object
	 * @return
	 * @throws JsonProcessingException
	 */
	public static String toUnderlineJSONString(Object object) throws JsonProcessingException{
		ObjectMapper mapper = new ObjectMapper();
		mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
		mapper.setSerializationInclusion(Include.NON_NULL);
		String reqJson = mapper.writeValueAsString(object);
		return reqJson;
	}

	/**
	 * 将下划线转换为驼峰的形式，例如：user_name-->userName
	 *
	 * @param json
	 * @param clazz
	 * @return
	 * @throws IOException
	 */
	public static <T> T toSnakeObject(String json, Class<T> clazz) throws IOException{
		ObjectMapper mapper = new ObjectMapper();            // mapper的configure方法可以设置多种配置（例如：多字段 少字段的处理）             //mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
		T reqJson =  mapper.readValue(json, clazz);
		return reqJson;
	}
}